#include <iostream>
#include <vector>

#include "../common/httplib.h"
#include "control.hpp"

using namespace httplib;
using namespace wjh_control;

int main()
{
    Server svr;
    Control ctl;

    // 1.获取题目列表
    svr.Get("/allQuestions", [&ctl](const Request &req, Response &resp) {
        //构建一张有全部题目的网页返回给客户
        std::string html;
        ctl.GetAllQuestions(html);
        resp.set_content(html, "text/html; charset=utf-8");
    });

    // 2.获取单个题目
    svr.Get(R"(/Question(\d+))", [&ctl](const Request &req, Response &resp) {
        std::string number = req.matches[1];
        std::string html;
        ctl.GetQuestion(number, html);
        resp.set_content(html, "text/html; charset=utf-8");
    });

    // 3.对提交的代码进行判题
    svr.Post(R"(/Judge/(\d+))", [&ctl](const Request &req, Response &resp) {
        std::string number = req.matches[1];
        std::string in_json = req.body;
        std::string out_json;
        std::cout << "选择成功" << std::endl;
        ctl.Judge(number, in_json,out_json);
        resp.set_content(out_json, "application/json; charset=utf-8");
    });

    svr.set_base_dir("./wwwroot");

    svr.listen("0.0.0.0", 8080);
    return 0;
}